import axios from 'axios';
import apiConfig from '../config/configIndex.js';
import mock from 'mockjs';
/**
 * 请求拦截器
 */
axios.interceptors.request.use(
  (config) => {
    // Do something before request is sent
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * 响应拦截器
 */
axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);
/**
 * 检查响应结果状态
 * @param response
 * @returns {*}
 */
function checkStatus(response) {
  if (response.status === 200 || response.status === 304) {
    return response;
  }
  return {
    data: {
      code: -404,
      message: response.statusText,
      data: response.statusText,
    },
  };
}
/**
 * 检查结果状态
 * @param res
 * @returns {*}
 */
function checkCode(res) {
  return res;
}
/**
 * 提交新增数据
 * @param url
 * @param data
 * @returns {Promise.<TResult>|*}
 */
function postData(url, data, cusHeader = {}) {
  const isMock = apiConfig.baseSetting.isMock == 'true';
  const template = data.template || '';
  if (isMock && template != null && template != undefined) {
    mock.mock(url, template);
  } else {
    data.template = '';
  }
  console.info(`cisdi post url==========${url}`);
  delete data.template;
  console.info(`cisdi post params==========${JSON.stringify(data)}`);
  const formalHeader = {
    'Access-Control-Allow-Origin': '*',
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/Json; charset=UTF-8',
  };
  return axios({
    method: 'post',
    url,
    data: JSON.stringify(data),
    params: [],
    timeout: 30000,
    headers: buildCusHeader(formalHeader, cusHeader),
  })
    .then(checkStatus)
    .then(checkCode);
}
/**
 * 提交更新数据
 * @param url
 * @param data
 * @returns {Promise.<TResult>|*}
 */
function putData(url, data, cusHeader = {}) {
  const isMock = apiConfig.baseSetting.isMock == 'true';
  const template = data.template || '';
  if (isMock && template != null && template != undefined) {
    mock.mock(url, template);
  } else {
    data.template = '';
  }
  console.info(`cisdi post url==========${url}`);
  delete data.template;
  console.info(`cisdi post params==========${JSON.stringify(data)}`);
  const formalHeader = {
    'Access-Control-Allow-Origin': '*',
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/json',
  };
  return axios({
    method: 'put',
    url,
    data: JSON.stringify(data),
    params: [],
    timeout: 30000,
    headers: buildCusHeader(formalHeader, cusHeader),
  })
    .then(checkStatus)
    .then(checkCode);
}

/**
 * 提交删除数据
 * @param url
 * @param data
 * @returns {Promise.<TResult>|*}
 */
function deleteData(url, data, cusHeader = {}) {
  const isMock = apiConfig.baseSetting.isMock == 'true';
  const template = data.template || '';
  if (isMock && template != null && template != undefined) {
    mock.mock(url, template);
  } else {
    data.template = '';
  }
  console.info(`cisdi post url==========${url}`);
  delete data.template;
  console.info(`cisdi post params==========${JSON.stringify(data)}`);
  const formalHeader = {
    'Access-Control-Allow-Origin': '*',
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/json',
  };
  return axios({
    method: 'delete',
    url,
    data: JSON.stringify(data),
    params: [],
    timeout: 30000,
    headers: buildCusHeader(formalHeader, cusHeader),
  })
    .then(checkStatus)
    .then(checkCode);
}

/**
 * 获取数据
 * @param url
 * @param params
 * @returns {Promise.<TResult>|*}
 */
function getData(url, data, cusHeader = {}) {
  const isMock = apiConfig.baseSetting.isMock == 'true';
  const template = data != null && data.template != null ? data.template : null;
  let params = {};
  if (isMock && template != null && template != undefined) {
    mock.mock(url, template);
    params = [];
  } else {
    data.template = '';
    params = data;
  }
  console.info(`cisdi post url==========${url}`);
  delete data.template;
  console.info(`cisdi post params==========${JSON.stringify(data)}`);
  const formalHeader = {
    'Access-Control-Allow-Origin': '*',
    'X-Requested-With': 'XMLHttpRequest',
  };
  return axios({
    method: 'get',
    url,
    data: [],
    params,
    timeout: 30000,
    dataType: 'json',
    headers: buildCusHeader(formalHeader, cusHeader),
  })
    .then(checkStatus)
    .then(checkCode);
}

/**
 * 构造自定义请求头
 * @param cusHeader
 */
function buildCusHeader(formalHeader = {}, cusHeader = {}) {
  const header = {};
  Object.assign(header, formalHeader, cusHeader);
  return header;
}
export default {
  getData,
  putData,
  postData,
  deleteData,
};
